package Actions;

import Beans.Relationship;
import Utils.Log;
import io.restassured.response.Response;
import org.testng.Assert;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;

public class BaseAction {
    public Log log=new Log(this.getClass());
    public static String URL="http://hc-t1.yonghuivip.com/app/api/";
    public static Map<String,String> cookies=new HashMap<String, String>();
    public Response response;
    public String resString;


    //查询供货关系
    public Response findRelationship(String shopcode,String skucode){
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .body("{\"pageindex\": 1, \"pagesize\": 5, \"skuinfoscopecode\": 524,\"shopcode\": \""+shopcode+"\", \"skucode\": \""+skucode+"\"}")
                .post(URL+"mms-msm-hub/supplierrelationship/findRelationshipList");
        String message=response.jsonPath().getString("data.message");
        Assert.assertEquals(message,"查找数据成功","没有查询到门店"+shopcode+"与商品"+skucode+"的供货关系！！");
        return response;
    }
    //查询供货关系
    public Relationship getRelationship(String shopcode,String skucode){
        Relationship relationship=new Relationship();

        response=findRelationship(shopcode,skucode);
        //System.out.println(response.asString());
        String message=response.jsonPath().getString("data.message");
        Assert.assertEquals(message,"查找数据成功","没有查询到门店"+shopcode+"与商品"+skucode+"的供货关系！！");
        relationship.setSpec(response.jsonPath().getString("data.rows[0].spec"));
        relationship.setProvidercode(response.jsonPath().getString("data.rows[0].suppliercode"));
        relationship.setUnitpricebeforetax(response.jsonPath().getString("data.rows[0].unitpricebeforetax"));
        relationship.setUnitpriceaftertax(response.jsonPath().getString("data.rows[0].unitpriceaftertax"));
        relationship.setRatecode(response.jsonPath().getString("data.rows[0].taxratecode"));
        relationship.setDepartmentcategorycode(response.jsonPath().getString("data.rows[0].categoryinfowrapper.departmentcategorycode"));
        relationship.setDepartmentcategoryname(response.jsonPath().getString("data.rows[0].categoryinfowrapper.departmentcategoryname"));
        relationship.setSmallcategorycode(response.jsonPath().getString("data.rows[0].categoryinfowrapper.smallcategorycode"));
        relationship.setSmallcategoryname(response.jsonPath().getString("data.rows[0].categoryinfowrapper.smallcategoryname"));
        relationship.setMiddlecategorycode(response.jsonPath().getString("data.rows[0].categoryinfowrapper.middlecategorycode"));
        relationship.setMiddlecategoryname(response.jsonPath().getString("data.rows[0].categoryinfowrapper.middlecategoryname"));
        relationship.setMaxcategorycode(response.jsonPath().getString("data.rows[0].categoryinfowrapper.bigcategorycode"));
        relationship.setMaxcategoryname(response.jsonPath().getString("data.rows[0].categoryinfowrapper.bigcategoryname"));
        relationship.setUnitcode(response.jsonPath().getString("data.rows[0].skumeasurementunits[0].unitcode"));
        relationship.setUnitname(response.jsonPath().getString("data.rows[0].skumeasurementunits[0].unitname"));
        relationship.setPurchasecode(response.jsonPath().getString("data.rows[0].purchasecode"));
        relationship.setAddresssettlecode(response.jsonPath().getString("data.rows[0].settlementlocationcode").substring(1));
        relationship.setSkuname(response.jsonPath().getString("data.rows[0].skuname"));
        relationship.setLogisticsmode(response.jsonPath().getString("data.rows[0].logisticsmode"));
        return relationship;
    }

    //查询商品门店档案
    public Response findskushop(String shopcode,String skucode){
        response=given()
                .contentType("application/json")
                .cookies(cookies)
                .body("{\"size\": 50, \"page\": 1, \"code\": \"9D15\", \"skucodes\": [\""+skucode+"\"], \"shopcode\": [\""+shopcode+"\"], \"searchscore\": 29}")
                .post(URL+"mms-es-hub/skushop/findskushop");

        return response;

    }
    //根据税码查询税率
    public Response findTaxation(String taxcode){
        response=given()
                .contentType("application/json")
                .cookies(cookies)
                .queryParams("pagesize",50,"pageindex",1,"taxcode",taxcode)
                //.body("{\"pagesize\": 50,\"pageindex\":1,\"taxcode\": \""+taxcode+"\"}")
                .post(URL+"mms-finance-center/taxation/listbypage");
        return response;
    }

    public String getResCode(Response response) {
        return response.jsonPath().getString("code");
    }

    public void waitSecond(int second ) {
        try {
            Thread.sleep(second * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
